OPTIONS NOFMTERR MPRINT PS=60 LS=80 sasautos =macro;
FILENAME macro "/disk/agedisk2/medicare.work/newhouse-DUA19577/jeff/programs/macro";


libname r '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru';
libname wk2 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2007';
libname perm '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab';
libname tab '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/price_tables';
libname pm2  '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/patient_cohorts';
libname perm7 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab/2007';
libname pb '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab/partb/2007';
libname ip '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab/ip/2007';
libname op '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/diab/op/2007';
libname wk '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/MA_files';



data hmo;
 set perm7.hmo_diab_plan2;
 len = length(varname);
 comorb = substr(varname, len, 1);
 type = substr(varname, len-2,1);
 if comorb = 'c' then comorb = 1; else comorb = 0;
 if type = '1' then type1 = 1; else type1 = 0;
 vb = substr(varname,1,6);
 if vb = "ed_vis";
 if hmo_pats ~= .;

proc means sum;
 var hmo_pats;

proc freq;
 tables comorb type;
 weight hmo_pats;

proc sql;
 create table h as select org_id, vb,
 sum(hmo_pats) as hmo_pats from hmo
 group by org_id, vb
 order by org_id, vb;


data hmo;
 set wk.hedis07_rru (keep =  bene_county_cd state_code zip5 newagec gender racec org_id
  cdc_d1_sum cdc_n1_sum  cdc_d3_sum cdc_n3_sum cdc_d4_sum cdc_n4_sum);

link = compress(racec||gender||newagec||zip5);
link_cty = compress(racec||gender||newagec||bene_county_cd||state_code);
link_st = compress(racec||gender||newagec||state_code);
 where state_code <= "53" and state_code ~in ("40", "48");

if cdc_d1_sum >= 1 or cdc_d3_sum >= 1 or cdc_d4_sum >= 1;
 count = max(cdc_d1_sum, cdc_d3_sum, cdc_d4_sum);


 if state_code in ("20", "30", "47", "22", "41", "07") then region=1; 
 else if state_code in ("33", "31", "39") then region=1; 
 else if state_code in ("23", "36", "14", "15", "52") then region=2; 
 else if state_code in ("24", "26", "16", "35", "43", "28", "17") then region=2; 
 else if state_code in ("08", "21", "09", "51", "49", "34", "42", "11", "10") then region=3; 
 else if state_code in ("18", "44", "01", "25") then region=3; 
 else if state_code in ("04", "19", "37", "45") then region=3; 
 else if state_code in ("27", "13", "53", "29", "46", "06", "03", "32") then region=4; 
 else if state_code in ("50", "38", "05", "12", "02") then region=4; 

proc sort;
 by org_id;

data hmo;
 merge hmo (in=hh) h (in=r);
 by org_id;
 if hh=1 and  r=1;


proc freq;
 tables region newagec gender racec ;
 weight count;






 
